package com.library.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.library.dao.DAOFactory;
import com.library.dao.MySQLDAOFactory;
import com.library.dao.User;
import com.library.dao.UserDAO;

/**
 * Servlet implementation class CreateUserServlet
 */
@WebServlet("/CreateUserServlet")
public class CreateUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CreateUserServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		int userid = Integer.parseInt(request.getParameter("userid"));
		String email = request.getParameter("email");
		String name = request.getParameter("username");
		String address = request.getParameter("address");
		String phone = request.getParameter("phone");
		String password = request.getParameter("password");
		String password2 = request.getParameter("password2");

		DAOFactory factory = new MySQLDAOFactory();
		UserDAO userDAO = factory.getUserDAO();
		User user = userDAO.findUser(userid);

		if (user == null) {
			if (password.equals(password2)) {
				user = new User();
				user.setAddress(address);
				user.setEmail(email);
				user.setName(name);
				user.setPassword(password);
				user.setPhone(phone);
				user.setUserId(userid);
				userDAO.insertUser(user);

				request.setAttribute("msg", "Đã tạo user");
				request.setAttribute("lastpage", "librarian/createuser.jsp");
				String add = "librarian/librariansuccesspage.jsp";
				RequestDispatcher dispatcher = request
						.getRequestDispatcher(add);
				dispatcher.forward(request, response);
			} else {
				request.setAttribute("error", "Mật khẩu nhập lại không khớp");
				String add = "librarian/librarianerrorpage.jsp";
				request.setAttribute("lastpage", "librarian/createuser.jsp");
				RequestDispatcher dispatcher = request.getRequestDispatcher(add);
				dispatcher.forward(request, response);
			}
		} else {
			request.setAttribute("error", "ID đã đăng ký");
			String add = "librarian/librarianerrorpage.jsp";
			request.setAttribute("lastpage", "librarian/createuser.jsp");
			RequestDispatcher dispatcher = request.getRequestDispatcher(add);
			dispatcher.forward(request, response);
		}
	}

}
